package com.qf.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.entity.City;
import com.qf.entity.R;
import com.qf.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * (City)表控制层
 *
 * @author makejava
 * @since 2020-11-12 09:40:17
 */
@RestController
@RequestMapping("/city")
@Slf4j
public class CityController {

    /**
     * 服务对象
     */
    @Autowired
    private CityService cityService;

    @Value("${page.size}")
    private Integer size;


    @RequestMapping("/list")
    public R<Page<City>> cityList(Integer cpage){
        //分页对象
        Page<City> page = new Page<>();

        if (cpage != null && cpage == -1) {
            List<City> citys = cityService.list();
            page.setRecords(citys);
        } else {
            page.setCurrent(cpage == null ? 1 : cpage);//当前第几页
            page.setSize(size);//每页显示多少条
            //执行分页查询
            page = cityService.page(page);

            System.out.println("当前第几页：" + page.getCurrent());
            System.out.println("每页多少条：" + page.getSize());
            System.out.println("共有多少条：" + page.getTotal());
            System.out.println("共有多少页：" + page.getPages());
            System.out.println("当页的数据：" + page.getRecords());
        }

        return new R<Page<City>>().setData(page);
    }


    @RequestMapping("/insert")
    public R<Boolean> cityInsert(@RequestBody City city){
        log.info("[cityinsert] 城市服务 新增城市！参数：" + city);
        boolean flag = cityService.save(city);
        return new R<Boolean>().setData(flag);
    }

    @RequestMapping("/getCityById")
    public R<City> getCityById(Integer cid){
        City city = cityService.getById(cid);
        return new R<City>().setData(city);
    }
}